﻿using System.Collections;

namespace System.ComponentModel.DataAnnotations;

/// <summary>
/// Custom validation attribute to validate list count.
/// </summary>
/// <param name="minCount"> Minimum count of the list. </param>
/// <param name="maxCount"> Maximum count of the list. </param>
public class ListCountValidator(int minCount, int maxCount) : ValidationAttribute
{
    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
    {
        if (value is IList list)
        {
            if (list.Count < minCount || list.Count > maxCount) return new ValidationResult($"List count must be between {minCount} and {maxCount}.");
        }

        return ValidationResult.Success;
    }
}
